<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<style>
    /* .element{
        overflow: auto;
    } */
    .content{
        height: 2000px;
    }
    .gotop{
        width: 100px;
        height: 100px;
        background-color: #2a3a24;
        text-align: center;
        line-height: 100px;
        position:fixed;
        bottom: 100px;
        right: 20px;
        display: none;
    }
</style>
<body>
    <div class="content">
        <p>返回首页</p>
        <div class="gotop">返回</div>
    </div>

    <script>
        let totop = document.getElementsByClassName('gotop')[0];
        window.onscroll = function(){
        let  res =  document.body.scrollTop || document.documentElement.scrollTop;
        console.log(res);
        if(res>400){
            totop.style.display = 'block';
        }else{
            totop.style.display = 'none'
        }
        }

        totop.onmouseover = function () {
            totop.style.backgroundColor ='#cb0707';
        }
        totop.onmouseout = function(){
            totop.style.backgroundColor ='#43fd00';
        }
        totop.onclick = function(){
            document.body.scrollTop =0;
            document.documentElement.scrollTop = 0;
        }
    </script>
</body>
</html>